package cn.iocoder.yudao.module.mon.ext_service.ys7_service.impl;

import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.module.common.util.http.HeaderConfig;
import cn.iocoder.yudao.module.common.util.http.HttpUtil;
import cn.iocoder.yudao.module.mon.common.ApiResponse;
import cn.iocoder.yudao.module.mon.common.ApiSimResponse;
import cn.iocoder.yudao.module.mon.dal.dataobject.Ys7AccessTokenResDO;
import cn.iocoder.yudao.module.mon.dal.redis.ys7auth.Ys7AccessTokenRedisDAO;
import cn.iocoder.yudao.module.mon.ext_service.ys7_service.Ys7ExtService;
import cn.iocoder.yudao.module.mon.vo.reponse.Ys7AccessTokenResVo;
import cn.iocoder.yudao.module.mon.vo.reponse.Ys7GetDeviceInfoResVo;
import cn.iocoder.yudao.module.mon.vo.reponse.Ys7GetDeviceListResVo;
import cn.iocoder.yudao.module.mon.vo.request.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.module.mon.constants.Ys7InterfaceConstans.*;
@Service
@Validated
@Slf4j
public class Ys7ExtServiceImpl implements Ys7ExtService {

    @Autowired
    private Ys7AccessTokenReqVo accessTokenReqVo;
    /**
     * 口令令牌
     */
    public static String ACCESS_TOKEN = "";
    private static HeaderConfig headerConfig = new HeaderConfig();
    static {
        headerConfig.addHeader("Content-Type", "application/x-www-form-urlencoded");
    }

    @Resource
    Ys7AccessTokenRedisDAO ys7AccessTokenRedisDAO;

    /**
     * 获取萤石云accessToken
     * token 7天有效 到期自动获取
     */
    @Override
    public Ys7AccessTokenResVo getAccessToken() {
        String url = API_ROOT_URL+LAPP_TOKEN_GET_POST;
        ApiSimResponse<Ys7AccessTokenResVo> apiResponse = postSimResult(url,accessTokenReqVo );
        Ys7AccessTokenResVo data =JSONUtil.toBean(JSONUtil.toJsonStr(apiResponse.getData()), Ys7AccessTokenResVo.class);

        Ys7AccessTokenResDO ys7AccessTokenResDO = new Ys7AccessTokenResDO();
        ys7AccessTokenResDO.setAccessToken(data.getAccessToken());
        ys7AccessTokenResDO.setExpireTime(data.getExpireTime());

        ys7AccessTokenRedisDAO.set(ys7AccessTokenResDO);
        return  data;
    }

    /**
     * 缓存获取萤石云accessToken
     * token 7天有效 到期自动获取
     */
    @Override
    public String getAccessTokenCache() {
        return ys7AccessTokenRedisDAO.get("").getAccessToken();
    }

    /**
     * 设备基础信息查询
     *
     * @param req 参数
     * @return result
     */
    @Override
    public Ys7GetDeviceInfoResVo getDeviceInfo(Ys7GetDeviceInfoReqVo req) {
//        Ys7AccessTokenResVo accessToken = getAccessToken();
//        req.setAccessToken(accessToken.getAccessToken());
        req.setAccessToken(ys7AccessTokenRedisDAO.get("").getAccessToken());
        String url = API_ROOT_URL+DEVICE_SEARCH_DEVICE_INFO_PSOT;
        ApiResponse<Ys7GetDeviceInfoResVo> apiResponse = postResult(url,req);
        Ys7GetDeviceInfoResVo result =JSONUtil.toBean(JSONUtil.toJsonStr(apiResponse.getResult().getData()), Ys7GetDeviceInfoResVo.class);

        return  result;
    }

    /**
     * 添加设备
     *
     * @param req 参数
     * @return result
     */
    @Override
    public void addDeviceInfo(Ys7AddDeviceReqVo req) {
//        Ys7AccessTokenResVo accessToken = getAccessToken();
//        req.setAccessToken(accessToken.getAccessToken());
        req.setAccessToken(ys7AccessTokenRedisDAO.get("").getAccessToken());
        String url = API_ROOT_URL+LAPP_DEVICE_ADD_POST;
        ApiSimResponse post = post(url, req);

    }

    /**
     * 删除设备
     *
     * @param req 参数
     * @return result
     */
    @Override
    public void deleteDeviceInfo(Ys7DeleteDeviceReqVo req) {
//        Ys7AccessTokenResVo accessToken = getAccessToken();
//        req.setAccessToken(accessToken.getAccessToken());
        req.setAccessToken(ys7AccessTokenRedisDAO.get("").getAccessToken());
        String url = API_ROOT_URL+LAPP_DEVICE_DELETE_POST;
        ApiSimResponse post = post(url, req);

    }

    /**
     * 获取设备列表
     *
     * @param req 参数
     * @return result
     */
    @Override
    public  List<Ys7GetDeviceListResVo> getDeviceList(Ys7DeleteDeviceReqVo req) {
//        Ys7AccessTokenResVo accessToken = getAccessToken();
//        req.setAccessToken(accessToken.getAccessToken());
                req.setAccessToken(ys7AccessTokenRedisDAO.get("").getAccessToken());
        String url = API_ROOT_URL+LAPP_DEVICE_LIST_POST;
        ApiResponse<List<Ys7GetDeviceListResVo>> apiResponse = postResult(url,  req);
        List<Ys7GetDeviceListResVo> result =JSONUtil.toBean(JSONUtil.toJsonStr(apiResponse.getResult().getData()), List.class);

        return  result;
    }

    /**
     * 修改设备名称
     * @param req param
     * @return
     */
    @Override
    public void updateDeviceName(Ys7UpdateNameDeviceReqVo req) {
//        Ys7AccessTokenResVo accessToken = getAccessToken();
//        req.setAccessToken(accessToken.getAccessToken());
        req.setAccessToken(ys7AccessTokenRedisDAO.get("").getAccessToken());

        String url = API_ROOT_URL+LAPP_DEVICE_NAME_UPDATE_POST;
       post(url,  req);


    }



    /**
     * get请求
     * @param url
     * @param param
     * @return
     */
    public static ApiSimResponse get(String url, String param){
        String response =  HttpUtil.post(url,headerConfig,param);
        ApiSimResponse apiResponse = JSONUtil.toBean(response, ApiSimResponse.class);
        checkResponse(apiResponse);
        return apiResponse;
    }
    /**
     * get请求
     * @param url
     * @param obj
     * @return
     */
    public static ApiSimResponse get(String url, Object obj){
        Map param = JSONUtil.toBean(JSONUtil.toJsonStr(obj),Map.class);
        String response =  HttpUtil.get(url,headerConfig,param);
        ApiSimResponse apiResponse = JSONUtil.toBean(response, ApiSimResponse.class);
        checkResponse(apiResponse);
        return apiResponse;
    }

    /**
     * get请求
     * @param url
     * @param param
     * @return
     */
    public static ApiResponse getResult(String url, String param){
        String response =  HttpUtil.post(url,headerConfig,param);
        ApiResponse apiResponse = JSONUtil.toBean(response, ApiResponse.class);
        checkResponse(apiResponse);
        return apiResponse;
    }
    /**
     * get请求
     * @param url
     * @param obj
     * @return
     */
    public static ApiResponse getResult(String url, Object obj){
        Map param = JSONUtil.toBean(JSONUtil.toJsonStr(obj),Map.class);
        String response =  HttpUtil.get(url,headerConfig,param);
        ApiResponse apiResponse = JSONUtil.toBean(response, ApiResponse.class);
        checkResponse(apiResponse);
        return apiResponse;
    }

    /**
     * post请求
     * @param url
     * @param param
     * @return
     */
    public static ApiSimResponse post(String url, String param){
        String response = HttpUtil.post(url,headerConfig,param);
        ApiSimResponse apiResponse = JSONUtil.toBean(response, ApiSimResponse.class);
        checkResponse(apiResponse);

        return apiResponse;
    }

    /**
     * post请求
     * @param url
     * @param param
     * @return
     */
    public static ApiResponse postResult(String url, String param){
        String response = HttpUtil.post(url,headerConfig,param);
        ApiResponse apiResponse = JSONUtil.toBean(response, ApiResponse.class);
        checkResponse(apiResponse);

        return apiResponse;
    }
    /**
     * post请求
     * @param url
     * @param obj
     * @return
     */
    public static ApiSimResponse post(String url, Object obj){
        Map param = JSONUtil.toBean(JSONUtil.toJsonStr(obj),Map.class);
        String response = HttpUtil.post(url,headerConfig,param);
        ApiSimResponse apiResponse = JSONUtil.toBean(response, ApiSimResponse.class);
        checkResponse(apiResponse);

        return apiResponse;
    }
    /**
     * post请求
     * @param url
     * @param obj
     * @return
     */
    public static ApiSimResponse postSimResult(String url, Object obj){
        Map param = JSONUtil.toBean(JSONUtil.toJsonStr(obj),Map.class);
        String response = HttpUtil.post(url,headerConfig,param);
        ApiSimResponse apiResponse = JSONUtil.toBean(response, ApiSimResponse.class);
        checkResponse(apiResponse);

        return apiResponse;
    }
    /**
     * post请求
     * @param url
     * @param obj
     * @return
     */
    public static ApiResponse postResult(String url, Object obj){
        Map param = JSONUtil.toBean(JSONUtil.toJsonStr(obj),Map.class);
        String response = HttpUtil.post(url,headerConfig,param);
        ApiResponse apiResponse = JSONUtil.toBean(response, ApiResponse.class);
        checkResponse(apiResponse);

        return apiResponse;
    }
    /**
     * 校验响应码
     * @param response  响应保温
     */
    public static void checkResponse(ApiSimResponse response){

        String code =  response.getCode();
        String msg =  response.getMsg();
        if( !"200".equals(code)){
            throw ServiceExceptionUtil.exception(new ErrorCode(Integer.valueOf(response.getCode() ),response.getMsg()));
        }
    }
    /**
     * 校验响应码
     * @param response  响应
     */
    public static void checkResponse(ApiResponse response){

        String code =  response.getResult().getCode();
        String msg =  response.getResult().getMsg();
        // 操作成功 ，查询时 设备已被自己添加
        if( !"200".equals(code) ){
            throw ServiceExceptionUtil.exception(new ErrorCode(Integer.valueOf(code ),msg));
        }
    }
}
